package os;

import java.util.ArrayList;
import java.util.HashMap;
import javafx.beans.property.SimpleStringProperty;

/**
 *
 * @author Paulius Dambrauskas
 */
public abstract class Process {
    public  HashMap<String, String> regs = new HashMap<>();
    public int step = 0;
    public String extra;
    protected int state;
    protected int priority;
    protected int fatherId;
    protected int selfId;
    public ArrayList<Integer> children = new ArrayList<>();
    public ArrayList<String> createdResources = new ArrayList<>();
    public ArrayList<ResourceElement> givenResources = new ArrayList<>();
    abstract public void runProcess();
    public void create(){}
    public int getState(){
        return this.state;
    }
    public void setState(int state){
        if(priority <= 50 && this.state == 0 ){
            //System.out.print("Mazinam prioriteta");
            priority--;
            try{
              OS.processes.get(children.get(0)).setPriority(priority-10);
            } catch (Exception e){}
        }
        this.state = state;
        setStateValue(state);
        
    }
    public int getSelfId(){
        return this.selfId;
    }
    public void setSelfId(int id){
        this.selfId = id;
    }
    public int getPriority(){
        return this.priority;
    }
    public void setPriority(int priority){
        this.priority = priority;
    }
    public ResourceElement takeFromGivenResources(String name){
        /*ResourceElement ret = givenResources.get(name);
        givenResources.remove(name);*/
        int k = 0;
        while(k < givenResources.size()){
            if(givenResources.get(k).resourceName.equals(name)){
                ResourceElement ret = givenResources.get(k);
                givenResources.remove(k);
                return ret;
                //break;
            }
            k++;
        }
        return null;
    }
    public void setStep(int s){
        step = s;
        //setStepValue(s);
        //System.out.println("saasdddsafhwtk");
    }
    public void incStep(int s){
        setStep(step + s);
        //setStepValue(step);
    }
    public int getFatherId(){
        return fatherId;
    }
    //Nesamonės FX'ui
    private SimpleStringProperty stateCell = new SimpleStringProperty(OS.STATES[this.state]);
    private SimpleStringProperty stepCell = new SimpleStringProperty(step+"");
   
    public void setStateValue(int value){
        stateCell.set(OS.STATES[value]);
    }
    public SimpleStringProperty stateProperty(){
        return stateCell;
    }
    public SimpleStringProperty procNameProperty(){
        return new SimpleStringProperty(this.getClass().getName());
    }
    public SimpleStringProperty stepProperty(){
        return stepCell;
    }
    public void setStepValue(int value){
        stepCell.set(value+"");
    }
    
    public void changeStepCell(int value) {
        setStepValue(value);
    }
    
    public void step(){
       
    }
}